/* Portfolio GUI */

#pragma warning(disable: 4995)          // don't warn about deprecated functions
#define _CRT_SECURE_NO_DEPRECATE		// ditto

#include "../include/Port_Global.h"
#include "../include/Colorvar.h"
#include "../include/Portfolio.h"


char defaultmsg[100][256];
char menutext[50][256];
int messages = 0;

//Load Global Text Messages
void loadmsg()
{
	fstream fopen;
	fopen.open("data/Defaultmsg.txt",ios::in);
	while(!fopen.eof())
	{
		fopen.getline(defaultmsg[messages],256);
		messages++;
	}
}

//=====================================================================================//
//Main
//=====================================================================================//
void main()
{

	Portfolio *port = new Portfolio();
	port->setupAllegro();
	port->startGUI();
	return;

	FONT *font;
	set_uformat(U_UTF8);
	loadmsg();

	//Graphics package for the gui
	DATAFILE *GraphicPackA = load_datafile("data/GraphicPackA.dat");
	DATAFILE *Japanese_Fonts = load_datafile("data/japfont.dat");

	//Setup Extra Stuff
	setup();

	//Japanese Fonts
	font = (FONT *)Japanese_Fonts[0].dat;

	//Buffer for Double Buffer
	BITMAP * B_Buffer = create_bitmap(800,600);
	BITMAP * B_Color  = create_bitmap(800,600);
	BITMAP * B_Scratch= create_bitmap(800,600);
	BITMAP * B_Text   = create_bitmap(400,40);
	BITMAP * B_Shadow = create_bitmap(800,600);


	//=====================================================================================//
	//Fade in title screen then cross fade into the mainmenu GUI
	//=====================================================================================//
	struct Button buttons[6];
	int num_buttons = 6;

	for(int i = 0 ; i < 5; i++)
	{
		buttons[i].oldx = buttons[i].xpos = 40;
		buttons[i].oldy = buttons[i].ypos = 130 + (80*i);
		buttons[i].B_Normal		= (BITMAP *)GraphicPackA[Folderclose].dat;
		buttons[i].B_Rollover	= (BITMAP *)GraphicPackA[Folderopen].dat; 
		buttons[i].options = 0;
	}
	//3d Graphics
	buttons[0].options = MENUTEXTA;
	buttons[1].options = MENUTEXTB;
	buttons[2].options = MENUTEXTC;
	buttons[3].options = MENUTEXTD;
	buttons[4].options = MENUTEXTE;

	//Initialize Button Options
	for(int i = 0 ; i < 5; i++)
	{
		for(int j = 0; j < buttons[i].options; j ++)
		{
			buttons[i].text_option[j] = new char[30];
			strcpy(buttons[i].text_option[j],"");
		}
	}
	//LOAD MENU TEXT
	fstream openf;
	openf.open("data/Menutext.txt",ios::in);
	openf.getline(menutext[0],256);
	int menuopnum = 0;
	while(!openf.eof())
	{
		openf.getline(menutext[menuopnum],256);
		menuopnum++;
	}
	openf.close();
	openf.clear();
	for(int i = 0; i < MENUTEXTA; i++)
		strcpy(buttons[0].text_option[i],menutext[MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTB; i++)
		strcpy(buttons[1].text_option[i],menutext[MENUTEXTA + MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTC; i++)
		strcpy(buttons[2].text_option[i],menutext[MENUTEXTA + MENUTEXTB + MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTD; i++)
		strcpy(buttons[3].text_option[i],menutext[MENUTEXTA + MENUTEXTB + MENUTEXTC + MENUOPNUM+i]);
	for(int i = 0; i < MENUTEXTE; i++)
		strcpy(buttons[4].text_option[i],menutext[MENUTEXTA + MENUTEXTB + MENUTEXTC + MENUTEXTD + MENUOPNUM+i]);

	buttons[5].xpos = 700;
	buttons[5].ypos = 20;
	buttons[5].B_Normal		= NULL;
	buttons[5].B_Rollover	= (BITMAP *)GraphicPackA[Glow].dat; 
	//Initilize Buttons
	for(int i = 0; i < num_buttons; i++)
	{
		buttons[i].trans = 0;
		buttons[i].flag = 0;
		buttons[i].hide = 0;
		buttons[i].alpha = 255;
		if(buttons[i].B_Normal != NULL)
		{
			buttons[i].drawpt = buttons[i].B_Normal->h;
			buttons[i].B_Scratch = create_bitmap(buttons[i].B_Normal->h,buttons[i].B_Normal->w);
		}
		else
		{
			buttons[i].B_Scratch = create_bitmap(1,1);
		}
	}

	//BEGIN APPLICATION
	//Fade in
	clear_to_color(B_Color, makecol(255,255,255));
	fader(B_Color,(BITMAP *)GraphicPackA[Title].dat,B_Buffer,10);
	//CrossFade
	fader((BITMAP *)GraphicPackA[Title].dat,(BITMAP *)GraphicPackA[Mainmenu].dat,B_Buffer,10);

	int locked = 0;
	int quit = 0;
	int mouselocked = 0;
	int xm = 0, ym = 0;
	int fadetime = 0;
	int fadeflag = 0;

	//Start Main Loop
	while(!key[KEY_ESC] && quit == 0)//If the user hits escape, quit the program
	{

		while(Portfolio::ticks > 0)
		{

			if(mouse_b == 0)
				mouselocked = 0;

			//Check mouse
			for(int i = 0; i < num_buttons; i++)
			{

				if(buttons[i].trans == 0)
					buttons[i].flag = 0;

				if(buttons[i].hide == 1 && buttons[i].drawpt > 0)
					buttons[i].drawpt-=1;

				if(buttons[i].trans == 1 && buttons[i].ypos > 130)
					buttons[i].ypos-=5;

				if(buttons[i].ypos == 130 && buttons[i].trans == 1)
					buttons[i].trans = 2;

				if(buttons[i].trans == 2 && mouse_b & 2)
				{
					for(int j = 0; j < num_buttons; j++)
					{
						buttons[j].hide = 0;
						//Hack fix it later
						buttons[j].drawpt = 74;
					}

					buttons[i].trans = 0;
					buttons[i].flag = 0;
					buttons[i].ypos = buttons[i].oldy;
					locked = 0;
				}

				if(mouse_x > buttons[i].xpos && mouse_x < buttons[i].xpos + 60)
				{
					if(mouse_y > buttons[i].ypos && mouse_y < buttons[i].ypos + 60)
					{
						if(buttons[i].hide != 1 && buttons[i].trans != 1)
						{
							buttons[i].flag = 1;
							if(mouse_b == 1 && i == 5)
								quit = 1;								
						}
					}
				}
				//SUBMENU
				if(buttons[i].trans == 2)
				{
					ym = -1;
					for(int j = 0; j < buttons[i].options; j++)
					{
						if((mouse_x > 130 && mouse_x < 400) && (mouse_y > 50*j + 239 && mouse_y < 50*j + 279))
						{
							ym = j;
							if(mouse_b == 1 && mouselocked == 0)
							{
								mouselocked = 1;
								//Access Sub Sections
								for(int k = 0; k < num_buttons; k++)
									buttons[k].drawpt = 0 ;

								ym = -1;

								switch(i)
								{
								case 0: 
									switch(j)
									{
									case 0: 
										gamemodule("3DEFFECTS",GraphicPackA,font);
										break;
									case 1:
										videomodule("BLENDERDEMO",GraphicPackA,font);
										break;
									case 2:
										videomodule("3DSTUDIO",GraphicPackA,font);
										break;
									case 3: 
										gamemodule("TURN",GraphicPackA,font);
										break;
									}
									break;
								case 1:
									switch(j)
									{
									case 0:
										gamemodule("2DGAME",GraphicPackA,font);
										break;
									case 1:
										picmodule("2D",GraphicPackA,font);
										break;
									case 2:
										videomodule("PSDEMO",GraphicPackA,font);
										break;
									default:
										break;
									}
									break;
								case 2:
									switch(j)
									{
									case 0:
										gamemodule("SUPERSPRITEPACK",GraphicPackA,font);
										break;
									default:
										break;
									}
								case 3:
									switch(j)
									{
									case 0:
										resumemodule("RESUME",GraphicPackA,font);
										break;
									default:
										break;
									}
								case 4:
									switch(j)
									{
									case 0:
										picmodule("WATASHI",GraphicPackA,font);
										break;
									default:
										break;
									}
								default:
									break;
								}
							}//END if(mouse_b == 1)			
						}
					}
				}

				if(locked == 0)
				{
					if(mouse_x > buttons[i].xpos && mouse_x < buttons[i].xpos + 60)
					{
						if(mouse_y > buttons[i].ypos && mouse_y < buttons[i].ypos + 60)
						{
							if(buttons[i].hide != 1 && buttons[i].trans != 1)
							{
								buttons[i].flag = 1;
								if(mouse_b == 1 && mouselocked == 0)
								{
									mouselocked = 1;
									//Deactivate all the other buttons
									for(int j = 0; j < num_buttons; j++)
									{
										if(j != 5)											
											buttons[j].hide = 1;	//hide all buttons exept exit			
									}
									//Except for button hit					
									buttons[i].hide = 0;
									buttons[i].trans = 1;
									buttons[i].flag = 1;
									locked = 1;
								}
							}
						}
					}
				}//End if(locked == 0)
			}//End for(int i = 0; i < num_buttons; i++)


			if(mouse_b == 1 && mouselocked == 0)
				mouselocked = 1;

			fadetime++;
			if(fadetime > 100)
			{
				fadetime = 0;
				fadeflag++;
			}
			if(fadeflag == 8)
				fadeflag = 0;

			Portfolio::ticks --;
		}//End while(Portfolio::ticks > 0)

		//==========================BEGIN DRAWING====================================================//
		// Get the screen
		acquire_screen();

		//FIRST DRAW THE BACKDROP
		draw_sprite(B_Buffer,(BITMAP *)GraphicPackA[Mainmenu].dat,0,0);
		draw_sprite(B_Buffer,(BITMAP *)GraphicPackA[Splash].dat,50,140);

		fadepics(B_Buffer,fadeflag,fadetime);
		//DRAW FOLDER ICONS
		set_alpha_blender();
		for(int i = 0; i < num_buttons; i++)
		{
			//Draw the main folders
			if(buttons[i].hide == 0)
			{					
				if( i < MENUOPNUM)
				{
					textprintf(B_Buffer, font, 135, buttons[i].ypos + 35, C_MainFolderShadow,menutext[i]);
					textprintf(B_Buffer, font, 130, buttons[i].ypos + 30, C_MainFolder,menutext[i]);
				}

				if(buttons[i].flag == 0)
				{
					if(buttons[i].B_Normal != NULL)
						draw_trans_sprite(B_Buffer,buttons[i].B_Normal	,buttons[i].xpos,buttons[i].ypos);
				}
				else
				{
					if(buttons[i].B_Rollover != NULL)
						draw_trans_sprite(B_Buffer,buttons[i].B_Rollover,buttons[i].xpos,buttons[i].ypos);
					if(i == 5)
					{				
						textprintf(B_Buffer,font,702,532, C_MainFolderShadow, defaultmsg[8]);
						textprintf(B_Buffer,font,700,530, C_MainFolder, defaultmsg[8]);
					}

				}
			}
			else
			{
				if(buttons[i].B_Normal != NULL)
				{
					clear_bitmap(buttons[i].B_Scratch);
					blit(buttons[i].B_Normal,buttons[i].B_Scratch,0,0,0,0,buttons[i].B_Normal->w,buttons[i].drawpt);
					draw_trans_sprite(B_Buffer,buttons[i].B_Scratch,buttons[i].xpos,buttons[i].ypos);
				}
			}			
		}
		solid_mode();


		//DRAW TEXT AND LINES FOR SUBMENU 
		for(int i = 0; i < num_buttons; i++)
		{


			if(buttons[i].trans == 2)
			{
				clear_to_color(B_Shadow,makecol(255,0,255));
				clear_to_color(B_Text,makecol(255,0,255));

				//Text and Line
				do_line(B_Shadow,70,204,70,204 + (buttons[i].options * 50),4,circleline);

				//Highlight Box

				if(ym != -1)
				{	
					rect	(B_Shadow, 127, 50*ym + 237, 554, 50*ym + 267, makecol(255,255,255));	
					rectfill(B_Shadow, 130, 50*ym + 239, 550, 50*ym + 264, makecol(255,255,255));
				}	

				for(int j = 0; j < buttons[i].options; j++ )
				{
					do_line(B_Shadow,70, 50*j + 254, 120, 50*j + 254, 4, circleline);
					textout(B_Shadow,font, buttons[i].text_option[j],132,50*j + 243,makecol(0,0,0));
				}	

				//Render Final
				//fblend_trans(B_Shadow,B_Buffer,10,10,50);
				draw_sprite(B_Buffer,B_Shadow,0,0);		

				textprintf(B_Buffer,font,52,502, C_MainFolderShadow, defaultmsg[4]);
				textprintf(B_Buffer,font,50,500, C_MainFolder, defaultmsg[4]);
				textprintf(B_Buffer,font,52,532, C_MainFolderShadow, defaultmsg[3]);
				textprintf(B_Buffer,font,50,530, C_MainFolder, defaultmsg[3]);
			}
		}

		if(locked == 0)
		{
			textprintf(B_Buffer,font,52,532, C_MainFolderShadow, defaultmsg[2]);
			textprintf(B_Buffer,font,50,530, C_MainFolder, defaultmsg[2]);
		}

		rectfill(B_Buffer,mouse_x - 3,mouse_y - 3,mouse_x + 3, mouse_y + 3, makecol(0,0,0));	
		rectfill(B_Buffer,mouse_x - 2,mouse_y - 2,mouse_x + 2, mouse_y + 2, makecol(255,255,255));
		blit(B_Buffer,screen,0,0,0,0,800,600);	  
		release_screen();
	}

	//Fadeout
	blit(B_Buffer,B_Scratch,0,0,0,0,800,600);
	clear_to_color(B_Color,makecol(255,255,255));
	fader(B_Scratch,B_Color,B_Buffer,10);


	//=====================================================================================//
	//Free memory
	//=====================================================================================//
	for(int i = 0; i < num_buttons; i++)
	{
		if(buttons[i].B_Scratch != NULL)
		{
			destroy_bitmap(buttons[i].B_Scratch);
		}
	}
	destroy_bitmap(B_Text);
	destroy_bitmap(B_Shadow);
	destroy_bitmap(B_Color);
	destroy_bitmap(B_Scratch);
	destroy_bitmap(B_Buffer);
	unload_datafile(GraphicPackA);
}
END_OF_MAIN();

void circleline( BITMAP *B_Buffer, int x, int y, int d)
{
	circle(B_Buffer, x, y, d, makecol(0,0,0) );
}


void fader(BITMAP *B_image1, BITMAP *B_image2,BITMAP *B_Buffer,int speed)
{
	//CrossFade
	for(int i = 0; i < 256; i+=speed)
	{
		draw_sprite(B_Buffer, B_image1, 0, 0);
		//fblend_trans(B_image2,B_Buffer,0,0,i);
		blit(B_Buffer, screen, 0, 0, 0, 0, 800, 600);
		clear_bitmap(B_Buffer);
	}	
}

void startproc(char * program, char * direc)
{

	STARTUPINFO si;
	PROCESS_INFORMATION pi;

	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );


	// Start the child process. 
	if( !CreateProcess( NULL,   // No module name (use command line). 
		program, // Command line. 
		NULL,             // Process handle not inheritable. 
		NULL,             // Thread handle not inheritable. 
		FALSE,            // Set handle inheritance to FALSE. 
		0,                // No creation flags. 
		NULL,             // Use parent's environment block. 
		direc,             // Use parent's starting directory. 
		&si,              // Pointer to STARTUPINFO structure.
		&pi )             // Pointer to PROCESS_INFORMATION structure.
		) 
		// Wait until child process exits.
		WaitForSingleObject( pi.hProcess, INFINITE );

	// Close process and thread handles. 
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );
}

void triforce(BITMAP * B_Buffer,int x, int y,int color,int distance)
{
	int expand = distance;
	if(distance > 0)
		expand +=2;
	else
		expand -=2;

	triangle(B_Buffer, x , y-expand, x+expand, y, x-expand, y,makecol(0,0,0));
	triangle(B_Buffer, x , y-distance, x+distance, y, x-distance, y,color);
}

